package com.jh.paymentchannel.pojo.freeQuick;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.base.MoreObjects;
import org.springframework.data.jpa.domain.AbstractPersistable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;


/**
 * 商户可以选择的支付通道
 */
@Entity
@Table(name = "t_payment_channel")
public class PaymentChannel extends AbstractPersistable<Long> {

    private static final long serialVersionUID = 1L;

    /**
     * 商家接入的通道
     **/
    @Column(name = "channel_name")
    private String channelName;

    /**
     * 商家接入的通道code, 支付接口的时候提交， 唯一
     */
    @Column(name = "channel_code")
    private String channelCode;

    /**
     * 通道类型
     * 0是收单  1是提现
     */
    @Column(name = "channel_type")
    private String channelType;

    /**
     * 通道类别
     * <p>
     * 0 =  支付宝wap
     * 1 =  微信wap
     * 2 =  支付宝扫码
     * 3 =  微信扫码
     * 4 =  微信公众号支付
     * 5 = 快捷扫码
     */
    @Column(name = "real_pay_type")
    private String realPayType;

    @Column(name = "create_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Column(name = "update_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    public PaymentChannel() {
        super();
    }

    public PaymentChannel(String channelName, String channelCode) {
        this.channelName = channelName;
        this.channelCode = channelCode;
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }


    public String getChannelCode() {
        return channelCode;
    }


    public void setChannelCode(String channelCode) {
        this.channelCode = channelCode;
    }


    public String getChannelType() {
        return channelType;
    }


    public void setChannelType(String channelType) {
        this.channelType = channelType;
    }


    public Date getCreateTime() {
        return createTime;
    }


    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }


    public Date getUpdateTime() {
        return updateTime;
    }


    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }


    public String getRealPayType() {
        return realPayType;
    }


    public void setRealPayType(String realPayType) {
        this.realPayType = realPayType;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("id", getId())
                .add("channelName", channelName)
                .add("channelCode", channelCode)
                .add("channelType", channelType)
                .add("realPayType", realPayType)
                .add("createTime", createTime)
                .add("updateTime", updateTime)
                .toString();
    }

    public static PaymentChannel init(String channelName, String channelCode) {
        return new PaymentChannel(channelName, channelCode);
    }

    public static PaymentChannel init(PaymentChannelDTO channelDTO) {
        ChannelCode smartChannel = ChannelCode.of(channelDTO.getChannelCode());
        assert smartChannel != null;
        PaymentChannel paymentChannel = init(smartChannel.getDesc(), smartChannel.getValue());
        paymentChannel.setChannelType(channelDTO.getChannelType());
        paymentChannel.setRealPayType(channelDTO.getRealPayType());
        Date currDate = new Date();
        paymentChannel.setUpdateTime(currDate);
        paymentChannel.setCreateTime(currDate);
        return paymentChannel;
    }

    public PaymentChannel transform(PaymentChannelDTO channelDTO) {
        setChannelType(channelDTO.getChannelType());
        setRealPayType(channelDTO.getRealPayType());
        setUpdateTime(new Date());
        return this;
    }
}
